<?php

/*
 * +--------------------------------------------------------------------------+
 * | Zephir Language |
 * +--------------------------------------------------------------------------+
 * | Copyright (c) 2013-2017 Zephir Team and contributors |
 * +--------------------------------------------------------------------------+
 * | This source file is subject the MIT license, that is bundled with |
 * | this package in the file LICENSE, and is available through the |
 * | world-wide-web at the following url: |
 * | http://zephir-lang.com/license.html |
 * | |
 * | If you did not receive a copy of the MIT license and are unable |
 * | to obtain it through the world-wide-web, please send a note to |
 * | license@zephir-lang.com so we can mail you a copy immediately. |
 * +--------------------------------------------------------------------------+
 */
namespace Zephir\Optimizers\FunctionCall;

use Zephir\Call;
use Zephir\CompilationContext;
use Zephir\CompilerException;
use Zephir\CompiledExpression;
use Zephir\Optimizers\OptimizerAbstract;

/**
 * FuncGetArgOptimizer
 *
 * Optimizes calls to 'func_get_arg' using internal function
 */
class FuncGetArgOptimizer extends OptimizerAbstract
{

    /**
     *
     * @param array $expression
     * @param Call $call
     * @param CompilationContext $context
     * @return bool|CompiledExpression|mixed
     * @throws CompilerException
     */
    public function optimize(array $expression, Call $call, CompilationContext $context)
    {
        if (! isset($expression['parameters']) || count($expression['parameters']) != 1) {
            return false;
        }

        $call->processExpectedReturn($context);

        $symbolVariable = $call->getSymbolVariable(true, $context);
        if ($symbolVariable->isNotVariableAndString()) {
            throw new CompilerException("Returned values by functions can only be assigned to variant variables", $expression);
        }

        if ($call->mustInitSymbolVariable()) {
            $symbolVariable->initVariant($context);
        }

        $symbol         = $context->backend->getVariableCode($symbolVariable);
        $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);

        $context->headersManager->add('kernel/main');
        if ($context->backend->isZE3()) {
            $context->codePrinter->output('zephir_get_arg(' . $symbol . ', zephir_get_intval(' . $resolvedParams[0] . '));');
        } else {
            $context->codePrinter->output('zephir_get_arg(' . $symbol . ', zephir_get_intval(' . $resolvedParams[0] . ') TSRMLS_CC);');
        }
        return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
    }
}
